//
//  ViewController.m
//  04_Homework
//
//  Created by 徐昊 on 16/7/19.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#define WIDTH self.view.frame.size.width
@interface ViewController ()

@property (nonatomic, strong)AVAudioPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self config];
}

- (void)config {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    imageView.image = [UIImage imageNamed:@"{1B13CD84-A66C-7E19-8E05-073C4C1787B6}"];
    [self.view addSubview:imageView];
#pragma mark - 毛玻璃
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:effect];
    visualView.frame = [[UIScreen mainScreen] bounds];
    [imageView addSubview:visualView];
    visualView.alpha = 0.9;
#pragma mark - 初始化播放器
    NSString *path = [[NSBundle mainBundle] pathForResource:@"姚贝娜 - 战争世界" ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    NSLog(@"%g", self.player.duration);
    self.player.numberOfLoops = 10;
    // 歌曲名Label
    UILabel *songName = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 40, 25, 80, 30)];
    songName.text = @"战争世界";
    songName.textColor = [UIColor whiteColor];
    songName.textAlignment = NSTextAlignmentCenter;
    songName.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:songName];
    // 歌手Label
    UILabel *singer = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 40, 60, 80, 30)];
    singer.text = @"- 姚贝娜 -";
    singer.textColor = [UIColor whiteColor];
    singer.textAlignment = NSTextAlignmentCenter;
    singer.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:singer];
    // 左上角下拉菜单
    UIButton *pullLeft = [UIButton buttonWithType:UIButtonTypeSystem];
    pullLeft.frame = CGRectMake(15, 20, 70, 60);
    [pullLeft setImage:[UIImage imageNamed:@"player_btn_close_normal"] forState:UIControlStateNormal];
    pullLeft.tintColor = [UIColor whiteColor];
    [self.view addSubview:pullLeft];
    // 右上角菜单选项
    UIButton *menu = [UIButton buttonWithType:UIButtonTypeSystem];
    menu.frame = CGRectMake(WIDTH - 75, 20, 60, 60);
    [menu setImage:[UIImage imageNamed:@"player_btn_more_normal"] forState:UIControlStateNormal];
    menu.tintColor = [UIColor whiteColor];
    [self.view addSubview:menu];
    // 流畅
    UIButton *tone = [UIButton buttonWithType:UIButtonTypeSystem];
    tone.frame = CGRectMake(90, 100, 50, 25);
    [tone setImage:[UIImage imageNamed:@"player_btn_lc_sel_normal"] forState:UIControlStateNormal];
    tone.tintColor = [UIColor greenColor];
    [self.view addSubview:tone];
    // 独家
    UIButton *kind = [UIButton buttonWithType:UIButtonTypeSystem];
    kind.frame = CGRectMake(150, 100, 50, 25);
    [kind setImage:[UIImage imageNamed:@"player_btn_copyright_normal"] forState:UIControlStateNormal];
    kind.tintColor = [UIColor greenColor];
    [self.view addSubview:kind];
    // MV
    UIButton *MV = [UIButton buttonWithType:UIButtonTypeSystem];
    MV.frame = CGRectMake(210, 100, 50, 25);
    [MV setImage:[UIImage imageNamed:@"player_btn_mv_normal"] forState:UIControlStateNormal];
    MV.tintColor = [UIColor greenColor];
    [self.view addSubview:MV];
    // dts
    UIButton *dts = [UIButton buttonWithType:UIButtonTypeSystem];
    dts.frame = CGRectMake(270, 100, 50, 25);
    [dts setImage:[UIImage imageNamed:@"player_btn_dts_off"] forState:UIControlStateNormal];
    dts.tintColor = [UIColor grayColor];
    [self.view addSubview:dts];
    // 专辑图片
    UIImageView *albumPicture = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 2 - 150, 150, 300, 300)];
    [albumPicture setImage:[UIImage imageNamed:@"06532116-54AE-40F7-A0D8-6D25D5CD8C47"]];
    albumPicture.layer.cornerRadius = 150;
    albumPicture.layer.masksToBounds = YES;
    [self.view addSubview:albumPicture];
    // 歌词
    UILabel *lyric = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 80, 460, 160, 40)];
    lyric.text = @"战争世界 - 姚贝娜";
    lyric.textAlignment = NSTextAlignmentCenter;
    lyric.textColor = [UIColor whiteColor];
    [self.view addSubview:lyric];
    // pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 30, 500, 60, 30)];
    pageControl.numberOfPages = 3;
    pageControl.currentPage = 1;
    [self.view addSubview:pageControl];
    // UISlider
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(WIDTH / 2 - 140, 520, 280, 30)];
    [slider setMinimumTrackImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [slider setMaximumTrackImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
    [self.view addSubview:slider];
    // 当前播放进度时间
    UILabel *currentTime = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 140 - 50, 520, 50, 30)];
    currentTime.text = @"0:00";
    currentTime.textAlignment = NSTextAlignmentCenter;
    currentTime.textColor = [UIColor whiteColor];
    [self.view addSubview:currentTime];
    // 曲目的总时长
    UILabel *songTime = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 + 140, 520, 50, 30)];
    songTime.text = @"4:00";
    songTime.textColor = [UIColor whiteColor];
    songTime.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:songTime];
    // 播放键
    UIButton *play = [UIButton buttonWithType:UIButtonTypeSystem];
    play.frame = CGRectMake(WIDTH / 2 - 40, 560, 80, 80);
    [play setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    play.tintColor = [UIColor greenColor];
    [play addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:play];
    // 上一曲
    UIButton *previous = [UIButton buttonWithType:UIButtonTypeSystem];
    previous.frame = CGRectMake(WIDTH / 2 - 120, 570, 60, 60);
    [previous setImage:[UIImage imageNamed:@"player_btn_pre_normal"] forState:UIControlStateNormal];
    previous.tintColor = [UIColor greenColor];
    [self.view addSubview:previous];
    // 下一曲
    UIButton *next = [UIButton buttonWithType:UIButtonTypeSystem];
    next.frame = CGRectMake(WIDTH / 2 + 60, 570, 60, 60);
    [next setImage:[UIImage imageNamed:@"player_btn_next_normal"] forState:UIControlStateNormal];
    next.tintColor = [UIColor greenColor];
    [self.view addSubview:next];
    // 下载
    UIButton *download = [UIButton buttonWithType:UIButtonTypeSystem];
    download.frame = CGRectMake(WIDTH / 2 - 25, 650, 50, 50);
    [download setImage:[UIImage imageNamed:@"player_btn_download_pay_normal"] forState:UIControlStateNormal];
    download.tintColor = [UIColor grayColor];
    [self.view addSubview:download];
    // 循环
    UIButton *circulation = [UIButton buttonWithType:UIButtonTypeSystem];
    circulation.frame = CGRectMake(WIDTH / 2 - 85, 650, 50, 50);
    [circulation setImage:[UIImage imageNamed:@"player_btn_repeat_normal"] forState:UIControlStateNormal];
    circulation.tintColor = [UIColor grayColor];
    [self.view addSubview:circulation];
    // 喜欢
    UIButton *love = [UIButton buttonWithType:UIButtonTypeSystem];
    love.frame = CGRectMake(WIDTH / 2 - 145, 650, 50, 50);
    [love setImage:[UIImage imageNamed:@"player_btn_favorited_normal"] forState:UIControlStateNormal];
    love.tintColor = [UIColor redColor];
    [self.view addSubview:love];
    // 分享
    UIButton *share = [UIButton buttonWithType:UIButtonTypeSystem];
    share.frame = CGRectMake(WIDTH / 2 + 35, 650, 50, 50);
    [share setImage:[UIImage imageNamed:@"player_btn_share_highlight"] forState:UIControlStateNormal];
    share.tintColor = [UIColor grayColor];
    [self.view addSubview:share];
    // 歌曲列表
    UIButton *list = [UIButton buttonWithType:UIButtonTypeSystem];
    list.frame = CGRectMake(WIDTH / 2 + 95, 650, 50, 50);
    [list setImage:[UIImage imageNamed:@"playing_recommend_floder"] forState:UIControlStateNormal];
    list.tintColor = [UIColor grayColor];
    [self.view addSubview:list];
}
/** 点击播放键和暂停键进行切换 */
- (void)play:(UIButton *)play {
    [self.player play];
    [play removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [play addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [play setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
}
- (void)pause:(UIButton *)pause {
    [self.player pause];
    [pause removeTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [pause addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [pause setImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
